//
//  LabelsViewController.m
//  Arts1
//
//  Created by apple on 15/9/28.
//  Copyright © 2015年 Arts1. All rights reserved.
//

#import "LabelsViewController.h"
#import "LabelsCollectionViewCell.h"
#import "SetPhotoViewController.h"

@interface LabelsViewController ()


//接收数据
@property (nonatomic ,strong) NSMutableArray *arrayData;

@end

@implementation LabelsViewController

static NSString * const reuseIdentifier = @"Cell";

- (NSMutableArray *)arrayData
{
    if (_arrayData == nil)
    {
        self.arrayData = [NSMutableArray array];
    }
    return _arrayData;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.collectionView.backgroundColor = [UIColor greenColor];

    // Register cell classes
    [self.collectionView registerClass:[LabelsCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    [self getLabelData];
    
}

/**
 *  接收标签的值
 */
- (void)getLabelData
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(getLabelsData:) name:@"setLabelData" object:nil];
}

//根据通知中心接收值
- (void)getLabelsData:(NSNotification *)notification
{
    _strLabels = (NSMutableString *)notification.object;
    
    BOOL isAdd = YES;
    int index = 0;
    
    for (NSString *str in self.arrayData)
    {
        if ([str isEqualToString:_strLabels])
        {
            isAdd = NO;
            index = (int)[self.arrayData indexOfObject:str];
        }
        
    }
    
    if (isAdd)
    {
        if (_arrayData.count < 5)
        {
            [self.arrayData addObject:_strLabels];
        }
    }
    else
    {
        [self.arrayData removeObjectAtIndex:index];
    }

    [self.collectionView reloadData];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _arrayData.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LabelsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.Datalabels = (NSString *)_arrayData[indexPath.row];
   
    // Configure the cell
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark <UICollectionViewDelegate>

@end
